package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.DishMapper;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /*
    *新增菜品同时添加口味数据
    *
    * */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long id = dishDto.getId();//获取的菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((menu)->{
            menu.setDishId(id);
            return menu;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表中dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /*
    * 根据id查询口味信息和对应的菜品信息
    * */
    @Transactional
    public DishDto getByIdWithFlavor(Long id) {
        //1、查询菜品数据 select * from dish where id = ?
        Dish dish = this.getById(id);//通过id获取菜品信息
        //先new出该对象 DishDto dishDto = new DishDto();
        DishDto dishDto = new DishDto();
        // 把dish菜品数据 赋值/拷贝 给 dishDto对象 BeanUtils.copyProperties
        BeanUtils.copyProperties(dish,dishDto);
        //2、查询菜品对应的口味数据 select * from dish_flavor where dish_id = ?
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dish.getId());//当前菜品对应的口味信息
        List<DishFlavor> flavors = dishFlavorService.list(qw);
        return dishDto;
    }

    /*
    * 修改菜品信息和对应的口味信息
    * */
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 1、修改菜品dish表的数据
        this.updateById(dishDto);
        //取出菜品id Long dishId = dishDto.getId();
        Long dishDtoId = dishDto.getId();
        // 2、修改菜品的口味数据 如何完成？？
        // 2.1 先删除原有的口味数据: delete from dish_flavor where dish_id = ?
        LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dishDtoId);
        dishFlavorService.remove(qw);
        // 2.2 添加菜品口味数据 List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> flavors = dishDto.getFlavors();//获取菜品口味数据
        flavors = flavors.stream().map((menu)->{
            menu.setDishId(dishDtoId);
            return menu;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /*
    * 逻辑删除
    * *//*
    @Transactional
    public DishDto deleteByIds(Long ids) {
        Wrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.in
        dishFlavorService.remove(qw);
    }*/
}
